package com.yhjapp.onethingoneyard.onethingoneyard;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

/**
 * 忘记密码
 * Created by Administrator on 2017-11-28.
 */

public class ForgotPasswordLoginActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv_login_forgotPassword_next;
    private TextView tv_Verification_Code;
    private ImageView iv_forgot_password_back;
    private EditText et_login_forgot_phone;
    private EditText et_login_forgot_code;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_forgot_password);
        tv_login_forgotPassword_next= (TextView) findViewById(R.id.tv_login_forgotPassword_next);
        tv_login_forgotPassword_next.setOnClickListener(this);
        tv_Verification_Code= (TextView) findViewById(R.id.tv_Verification_Code);
        et_login_forgot_phone= (EditText) findViewById(R.id.et_login_forgot_phone);
        et_login_forgot_code= (EditText) findViewById(R.id.et_login_forgot_code);

        final CountDownTimerUtils undown=new CountDownTimerUtils(tv_Verification_Code,60000,1000);
        tv_Verification_Code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_login_forgot_phone.getText().toString().equals("")||et_login_forgot_phone.getText().toString().equals(null)||et_login_forgot_phone.getText().toString().equals("null")){
                    Toast.makeText(ForgotPasswordLoginActivity.this,"请输入手机号", Toast.LENGTH_SHORT).show();
                }else {
                    undown.start();
                    LoadLogincode();
                }
            }
        });
        iv_forgot_password_back= (ImageView) findViewById(R.id.iv_forgot_password_back);
        iv_forgot_password_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }



    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case  R.id.tv_login_forgotPassword_next:
               LoadLogindx();

               break;

       }
    }
    private void LoadLogindx(){

        String phone=et_login_forgot_phone.getText().toString();
        String code=et_login_forgot_code.getText().toString();
        if(phone.equals("")){
            Toast.makeText(ForgotPasswordLoginActivity.this,"", Toast.LENGTH_SHORT).show();
        }else if(code.equals("")){
            Toast.makeText(ForgotPasswordLoginActivity.this,"", Toast.LENGTH_SHORT).show();
        }
        Log.e("","WYBS"+ AUniqueIdentifier.getDeviceId(ForgotPasswordLoginActivity.this));
        String url = "http://develop.jhjvip.cn:8080/mall/mobile/login/in"+"?phone="+phone+"&code="+code+"&appId="+ AUniqueIdentifier.getDeviceId(ForgotPasswordLoginActivity.this);
        OkHttpUtils.get().url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                LoginBean list = gson.fromJson(response, LoginBean.class);
                if(list.getCode().equals("10000")) {

//                    Toast.makeText(ForgotPasswordLoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                    Intent inext=new Intent(ForgotPasswordLoginActivity.this,ForgotPasswordNextActivity.class);
                    inext.putExtra("token",list.getData().getToken());
                    startActivity(inext);
                    finish();
                }else {
                    Toast.makeText(ForgotPasswordLoginActivity.this, list.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }


        });
    }
    private void LoadLogincode(){

        String url = "http://develop.jhjvip.cn:8080/mall/mobile/login/code"+"?phone="+et_login_forgot_phone.getText().toString();
        OkHttpUtils.get().url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                RegisterBean list = gson.fromJson(response, RegisterBean.class);
                if(list.getCode().equals("10000")) {
                    Toast.makeText(ForgotPasswordLoginActivity.this, list.getMsg()+",请稍等...", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(ForgotPasswordLoginActivity.this, list.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }


        });
    }
}
